<template>
  <div class="user">
    <div class="user-left">
      <div class="user-img"><img :src="avatar" alt="" /></div>
      <!-- <p>{{ nickname }}</p> -->
      <p @click="user">个人中心</p>
      <p @click="mail">积分商城</p>
      <p @click="quit">退出</p>
    </div>
    <div class="user-right">
      <el-tabs type="border-card">
        <el-tab-pane label="个人信息">
          <div class="user-body">
            <div class="avatar"><img :src="avatar" /></div>
            <div class="Personal">
              <div class="Personal-input">
                姓名：<input type="text" :value="username" /> 昵称：<input
                  type="text"
                  :value="nickname"
                />
                学校：<input type="text" :value="school" /> 性别：<input
                  type="text"
                  :value="gender"
                />
                密码：<input type="password" :value="password" />
              </div>
            </div>
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              class="add"
              @click="show"
              >修改密码</el-button
            >
            <el-dialog
              append-to-body
              title="修改密码"
              width="600px"
              :before-close="handleClose"
              :visible.sync="dialogVisible"
            >
              <el-form
                :model="ruleForm"
                status-icon
                :rules="rules"
                ref="ruleForm"
                label-width="100px"
                class="demo-ruleForm"
              >
                <el-form-item label="旧密码" prop="oldPassword">
                  <el-input
                    type="password"
                    v-model="ruleForm.oldPassword"
                    autocomplete="off"
                  ></el-input>
                </el-form-item>
                <el-form-item label="新密码" prop="newPassword">
                  <el-input
                    type="newPassword"
                    v-model="ruleForm.newPassword"
                    autocomplete="off"
                  ></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPass">
                  <el-input
                    type="password"
                    v-model="ruleForm.checkPass"
                    autocomplete="off"
                  ></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="submitForm('ruleForm')"
                    >提交</el-button
                  >
                  <el-button @click="resetForm('ruleForm')" >取消</el-button>
                </el-form-item>
              </el-form>
            </el-dialog>
          </div>
        </el-tab-pane>
        <el-tab-pane label="兑换记录" name="second" @click="getChangeRecord()">
          <div  class="user-body"  v-for="item in list" :key="JSON.stringify(item)">
            <div class="msg" >
              <p>{{ item.name }}</p>
              <p>{{ item.exchangeDate | formatTime }}</p>
            </div>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>
<script>
import { mapState, mapMutations } from "vuex";
// import Moment from "moment"
export default {
  name: "user",
  data() {
    var validateoldPass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkoldPass !== "") {
          this.$refs.ruleForm.validateField("checkoldPass");
        }
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    // filters:{
    //   formatTime (value) {
    //     return Moment(value).format('YYYY-MM-DD hh:mm')
    //   }
    // }
    return {
      dialogVisible: false,
      list:[],
      ruleForm: {
        oldPassword: "",
        newPassword: "",
        checkPass: "",
      },
      rules: {
        oldPassword: [{ validator: validateoldPass, trigger: "blur" }],
        newPassword: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  computed: {
    ...mapState({
      username: (state) => state.login.username,
      password: (state) => state.login.password,
      age: (state) => state.login.age,
      avatar: (state) => state.login.avatar,
      gender: (state) => state.login.gender,
      school: (state) => state.login.school,
      nickname: (state) => state.login.nickname,
      id: (state) => state.login._id,
    }),
  },
  created() {
    // this.getList()
  },
  mounted() {
    // this.getChangeRecord();
  },
  methods: {
    ...mapMutations(["updateRegister","updateLogin"]),
    quit() {
      alert("确定退出？");
      if (alert) {
        //清空数据
        localStorage.removeItem('userInfo')
        this.$router.push({
          name: "Home",
        });
      }
    },
    // async getChangeRecord () {
    //   // console.log('===',this.user);
    //   const res = await this.yGet('/shop/exchangedRecord', {
    //     params: {
    //       id: this.user._id
    //     }
    //   })
    //   if(res){
    //     this.list = res;
    //   }
    // },
    returnRandom() {
      return Math.random()
    },
    // async getList() {
    //   const data = await this.yGet("/shop/exchangedRecord");
    //   if (data) {
    //     console.log("data", data);
    //     this.list = data;
    //   }
    // },
    mail() {
      this.$router.push({
        name: "mail",
      });
    },
    user() {
      this.$router.push({
        name: "user",
      });
    },
    show() {
      this.dialogVisible = true;
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
            const data = await this.yPut("/user/changePassword", {
              if(data){
                if(data.status == 200){
                 this.updateLogin(this.ruleForm.newPassword)
                }
              }
            });
            console.log("data",data);
            // alert("修改成功!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //   async getFun() {
    //   const data = await this.yGet("/shop/exchangedRecord");
    //   console.log(data.data);
    //   if (data) {
    //     if (data.status == 200) {
    //       this.list = data.data;
    //     }
    //   }
    // },
    async get() {
      const data = await this.yGet("/shop/shopList");
      console.log(data.data);
      if (data) {
        if (data.status == 200) {
          this.list = data.data;
        }
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(() => {
          done();
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
.user {
  padding: 0;
  margin: 0;
  &-left {
    width: 500px;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    background-color: #3d3b3b;
    margin: auto;
    padding-top: 100px;
    font-size: 35px;
    &-img {
      width: 50%;
      margin: auto;
    }
    p {
      width: 500px;
      height: 80px;
      padding-top: 45px;
      text-align: center;
      margin: auto;
    }
  }
  &-right {
    position: fixed;
    left: 500px;
    top: 0;
    right: 0;
    bottom: 0;
    font-size: 30px;
  }
  &-body {
    width: 90%;
    height: 900px;
    margin: auto;
    padding-bottom: 30px;
    border-radius: 5px;
    box-shadow: 0px 0px 2px 1px rgb(0 0 0 / 20%);
    text-align: center;
  }
}
img {
  width: 175px;
  height: 175px;
  border-radius: 50%;
  margin-left: 175px;
}
.el-button.is-circle {
  position: fixed;
  left: 60%;
  top: 66%;
  border-radius: 10%;
}
ul {
  list-style: none;
  width: 90%;
  padding: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.v-modal {
  background: #fff !important;
}
.msg {
  width: 80%;
  height: 70px;
  margin: auto;
  display: flex;
  justify-content: space-between;
  text-align: center;
  align-items: center;
  border-bottom: 1px solid #c3bfbf;
  padding-top: 50px;
}
.Personal {
  height: 400px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  text-align: center;
  margin: auto;
  padding-top: 150px;
  input {
    width: 310px;
    height: 50px;
    border: none;
    border-radius: 10px;
    margin-top: 60px;
    font-size: 30px;
  }
}
.avatar {
  position: fixed;
  left: 500px;
  top: 100px;
}
</style>